package list;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

public class Demo01 {
    public static void main(String[] args) {
        //准备实验素材
        ArrayList<Integer> list = new ArrayList<>();
        Integer[] array = new Integer[100000];
        Random random = new Random();
        for(int i=0; i<100000; i++){
            int n = random.nextInt();
            list.add(n);
            array[i]=n;
        }
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.addAll(list);
        //测试 get(0) 方法的性能
        test(array,list,linkedList, 0);
        //测试 get(25000)
        test(array,list,linkedList, 25000);
        //测试 get(50000)
        test(array,list,linkedList, 50000);
        //测试 get(100000-1)
        test(array,list,linkedList, 100000-1);

        //测试add
        addTest(list, linkedList, 0);
        addTest(list, linkedList, 50000);
        addTest(list, linkedList, 100000-1);

    }
    public static void test(Integer[] array,
                            ArrayList<Integer> list,
                            LinkedList<Integer> linkedList,
                            int index){
        long t0 = System.nanoTime();
        Integer i = array[index];
        long t1 = System.nanoTime();
        i = list.get(index);
        long t2 = System.nanoTime();
        i = linkedList.get(index);
        long t3 = System.nanoTime();
        //输出结果
        System.out.println("Array ["+index+"]:"+(t1-t0));
        System.out.println("ArrayList get("+index+"):"+(t2-t1));
        System.out.println("LinkedList get("+index+"):"+(t3-t2));
    }

    public static void addTest(
            ArrayList<Integer> list,
            LinkedList<Integer> linkedList,
            int index
    ){
        long t1 = System.nanoTime();
        list.add(index, 100);
        long t2 = System.nanoTime();
        linkedList.add(index, 100);
        long t3 = System.nanoTime();
        System.out.println("ArrayList add("+index+")" + (t2-t1));
        System.out.println("linkedList add("+index+")" + (t3-t2));
    }
}
